package com._520it.crm.service.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com._520it.crm.domain.Permission;
import com._520it.crm.domain.Role;
import com._520it.crm.mapper.RoleMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.QueryObject;
import com._520it.crm.service.IRoleService;

@Service
public class RoleServiceImpl implements IRoleService{
	@Autowired
	private RoleMapper mapper;
	@Override
	public int deleteByPrimaryKey(Long id) {
		return mapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Role record) {
		int count = mapper.insert(record);
		
		List<Permission> list = record.getPermissions();
		for (Permission permission : list) {
			mapper.insertRelation(record.getId(), permission.getId());
		}
		//处理中间表数据
		return count;
	}

	@Override
	public Role selectByPrimaryKey(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Role> selectAll() {
		return mapper.selectAll();
	}

	@Override
	public int updateByPrimaryKey(Role record) {
		return mapper.updateByPrimaryKey(record);
	}

	@Override
	public PageResult queryPageResult(QueryObject qo) {
		//查询结果集
		Long count = mapper.queryPageCount(qo);
		if (count>0) {
			return new PageResult(count, mapper.queryPageResult(qo));
		}
		return new PageResult(count, Collections.EMPTY_LIST);
	}
}
